package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrderPageDto;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.exception.CustomException;
import com.itheima.reggie.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author Lff
 * @date 2022/3/4
 */
@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {
    @Autowired
    private OrderService service;

    /**
     * 生成订单
     *
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders) {
        service.submit(orders);
        return R.success("付款成功！");
    }


    @GetMapping("/page")
    public R<Page> page(OrderPageDto order) {
        log.info("order===>{}", order.toString());
        Page<Orders> pages = service.findByPage(order);
        return R.success(pages);
    }

    @PutMapping
    public R<String> upStatus(@RequestBody Orders orders) {
        service.upStatus(orders);
        orders.toString();

        return R.success("修改成功!");
    }

    /**
     * C端订单显示
     *
     */
    @GetMapping("/userPage")
    public R<Page> userPage(Integer page,Integer pageSize){
        Page<OrdersDto> orders = service.findUserPage(page,pageSize);
        return R.success(orders);
    }

    /**
     * C端用户再来一单
     */
    @PostMapping("/again")
    public R<String> again(@RequestBody Orders orders){
        try {
            service.orderAgain(orders);
        } catch (Exception e) {
            throw new CustomException("哎呀，网络开小差了呢。。");
        }
        return R.success("已经添加到您的购物车啦");
    }
}
